from django.db import models
from utils.models import BaseModel


class CarModel(BaseModel):
    name = models.CharField(max_length=16, verbose_name='车型')

    class Meta:
        db_table = 'tb_car_model'
        verbose_name_plural = '车型表'

    def __str__(self):
        return self.name


class CarBrand(BaseModel):
    name = models.CharField(max_length=16, verbose_name='汽车品牌')

    class Meta:
        db_table = 'tb_car_brand'
        verbose_name_plural = '汽车品牌表'

    def __str__(self):
        print(type(self.name))
        return self.name


class Car(BaseModel):
    box_choice = (
        (1, '手动'),
        (2, '自动')
    )

    air_choice = (
        (1, '自然进气'),
        (2, '涡轮增压')
    )

    brand = models.ForeignKey(
        CarBrand,
        related_name='brand_car',
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="车牌"
    )
    model = models.ForeignKey(
        CarModel,
        related_name='model_car',
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="车型"
    )
    box = models.IntegerField(

        choices=box_choice,
        default=2,
        verbose_name='变速箱'
    )

    volume = models.IntegerField(
        verbose_name='油箱容量'
    )

    cc = models.DecimalField(
        max_digits=3,
        decimal_places=1,
        verbose_name='排量'
    )

    consumption = models.DecimalField(
        max_digits=3,
        decimal_places=1,
        verbose_name='百公里油耗'
    )

    seat = models.IntegerField(
        verbose_name='座位数'
    )

    air = models.IntegerField(
        choices=air_choice,
        default=1,
        verbose_name='进气方式'
    )

    price = models.IntegerField(
        verbose_name='日均价格'
    )

    store = models.ForeignKey(
        'store.Store',
        related_name='store_car',
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="门店"
    )

    img = models.ImageField(
        upload_to='car',
        null=True,
        verbose_name='汽车图片'
    )

    @property
    def box_name(self):
        return self.get_box_display()

    @property
    def get_store(self):
        return self.store.name

    @property
    def get_air(self):
        return self.get_air_display()


    class Meta:
        db_table = 'tb_car'
        verbose_name_plural = '汽车表'

    def __str__(self):
        return self.brand.name


